#include "../mqserver/mq_route.hpp"
#include "../mqcommon/mq_msg.pb.h"
#include <string>
#include <gtest/gtest.h>

class routeTest : public testing::Environment
{
public:
    virtual void SetUp() override
    {
    }
    virtual void TearDown() override
    {
    }
};

TEST(routeTest, legal_routing_test)
{
    std::string rkey1 = "news.music.pop";
    std::string rkey2 = "news..music.pop";
    std::string rkey3 = "news.music.pop.";
    std::string rkey4 = "news.music.jay_pop";
    ASSERT_EQ(wsmq::Router::IsLegalRoutingKey(rkey1), true);
    ASSERT_EQ(wsmq::Router::IsLegalRoutingKey(rkey2), true);
    ASSERT_EQ(wsmq::Router::IsLegalRoutingKey(rkey3), true);
    ASSERT_EQ(wsmq::Router::IsLegalRoutingKey(rkey4), true);
}

TEST(routeTest, legal_binding_test)
{
    std::string bkey1 = "news.#.pop";
    std::string bkey2 = "news.#.*music.pop";
    std::string bkey3 = "*.#.*.";
    std::string bkey4 = "news.#.#";
    ASSERT_EQ(wsmq::Router::IsLegalBindingKey(bkey1), true);
    ASSERT_EQ(wsmq::Router::IsLegalBindingKey(bkey2), false);
    ASSERT_EQ(wsmq::Router::IsLegalBindingKey(bkey3), false);
    ASSERT_EQ(wsmq::Router::IsLegalBindingKey(bkey4), false);
}

TEST(routeTest, route_test)
{
    std::vector<std::string> bkeys = {
        "aaa.bbb",
        "aaa.bbb",
        "aaa.bbb.#",
        "#.aaa",
        "*.aaa.cc"};
    std::vector<std::string> rkeys = {
        "aaa.bbb",
        "aaa.bbb.ccc",
        "aaa.bbb.ccc.ddd.eee",
        "aaa",
        "hhh.aAa.c_c"};
    std::vector<bool> results = {
        true,
        false,
        true,
        true,
        false};
    for (int i = 0; i < bkeys.size(); i++)
    {
        ASSERT_EQ(wsmq::Router::route(wsmq::ExchangeType::TOPIC, rkeys[i], bkeys[i]), results[i]);
    }
}

int main(int argc, char *argv[])
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new routeTest);
    return RUN_ALL_TESTS();
}
